cmake_minimum_required(VERSION 3.16)
project(AudioQualityTools)

# 设置默认构建类型为Release（如果没有指定）
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# 设置优化选项
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    # 高优化等级
    set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native -flto")
    set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native -flto")
    message(STATUS "Building with high optimization (-O3, -march=native, -mtune=native, -flto)")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    # 调试模式
    set(CMAKE_C_FLAGS_DEBUG "-O0 -g -DDEBUG")
    set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DDEBUG")
    message(STATUS "Building with debug flags (-O0, -g)")
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
    # 带调试信息的优化版本
    set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
    message(STATUS "Building with release + debug info (-O2, -g)")
endif()

# 启用链接时优化（LTO）
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    include(CheckIPOSupported)
    check_ipo_supported(RESULT ipo_supported)
    if(ipo_supported)
        set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
        message(STATUS "Link Time Optimization (LTO) enabled")
    else()
        message(WARNING "Link Time Optimization (LTO) not supported by compiler")
    endif()
endif()

# 添加编译器特定的优化选项
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        # GCC/Clang 特定优化（移除 -ffast-math 以避免 Armadillo 警告）
        add_compile_options(
            -funroll-loops       # 循环展开
            -fprefetch-loop-arrays # 循环数组预取
            -fomit-frame-pointer # 省略帧指针
        )
        message(STATUS "Added GCC/Clang specific optimizations (without -ffast-math)")
    endif()
endif()

# 为子项目传递优化设置
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "C flags" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "CXX flags" FORCE)

# 添加子目录
add_subdirectory(p563)
add_subdirectory(itu-t-pesq)
add_subdirectory(visqol)

